package com.east.sng.controller;

import com.east.sng.entity.param.MenuParam;
import com.east.sng.entity.result.MenuFind;
import com.east.sng.service.MenuService;
import com.east.sng.utils.ValidationUtil;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author east
 * @date: 2021/1/5
 * @Description:
 **/
@Controller
@RequiresRoles("0")
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    /**
     * 菜单信息页面跳转
     * @param menuName
     * @param parentName
     * @param mlevel
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @RequiresPermissions("menu:list")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(String menuName,Integer parentName,Integer mlevel, @RequestParam(name="flag",defaultValue="0")Integer flag,
                       @RequestParam(name="pn",defaultValue="1")Integer pageNum,
                       @RequestParam(name = "pSize", defaultValue = "5") Integer pageSize, Model model){
        Map<String, Object> params = new HashMap();
        params.put("menuName",menuName);
        params.put("parentName",parentName);
        params.put("mlevel",mlevel);
        params.put("flag",flag);
        model.addAttribute("menuParents",this.menuService.findParentMenus());
        model.addAttribute("page",PageHelper.startPage(pageNum,pageSize));
        model.addAttribute("menus",this.menuService.findMenus(params));
        return "system/menuList";
    }

    /**
     * 菜单录入信息查询
     * @return
     */
    @RequiresPermissions("menu:add")
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    @ResponseBody
    public List<Map<String, Object>> toAddMenu(){
        return this.menuService.findParentMenus();
    }

    /**
     * 菜单录入
     * @param menu
     * @param br
     * @return
     */
    @RequiresPermissions("menu:add")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public String addMenu(@Valid MenuParam menu, BindingResult br){
        ValidationUtil.validateData(br);
        this.menuService.addMenu(menu);
        return "success";
    }

    /**
     * 菜单修改信息查询
     * @param menuId
     * @return
     */
    @RequiresPermissions(value = {"menu:list","menu:add"},logical = Logical.AND)
    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    @ResponseBody
    public MenuFind toEditMenu(Integer menuId){
        MenuFind menu = this.menuService.findMenu(menuId);
        if (menu!=null) {
            menu.setMenuParents(this.menuService.findParentMenus());
        }
        return menu;
    }

    /**
     * 菜单信息修改
     * @param menu
     * @param br
     * @return
     */
    @RequiresPermissions(value = {"menu:list","menu:add"},logical = Logical.AND)
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @ResponseBody
    public String editMenu(@Valid MenuParam menu, BindingResult br){
        ValidationUtil.validateData(br);
        this.menuService.editMenu(menu);
        return "success";
    }

    /**
     * 菜单消息删除
     * @param id
     * @return
     */
    @RequiresPermissions(value = {"menu:list","menu:add"},logical = Logical.AND)
    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @ResponseBody
    public String removeMenu(Integer id){
        return this.menuService.removeMenu(id)?"success":"fail";
    }

    /**
     * 菜单信息恢复
     * @param id
     * @return
     */
    @RequiresPermissions(value = {"menu:list","menu:add"},logical = Logical.AND)
    @RequestMapping(value = "/recover",method = RequestMethod.POST)
    @ResponseBody
    public String recoverMenu(Integer id){
        return this.menuService.recoverMenu(id)?"success":"fail";
    }

    /**
     * 菜单信息批量删除
     * @param ids
     * @return
     */
    @RequiresPermissions(value = {"menu:list","menu:add"},logical = Logical.AND)
    @RequestMapping(value = "/removes",method = RequestMethod.POST)
    @ResponseBody
    public String removeMenus(Integer[] ids){
        return this.menuService.batchRemoveMenus(Arrays.asList(ids))?"success":"fail";
    }

    /**
     * 菜单信息批量恢复
     * @param ids
     * @return
     */
    @RequiresPermissions(value = {"menu:list","menu:add"},logical = Logical.AND)
    @RequestMapping(value = "/recovers",method = RequestMethod.POST)
    @ResponseBody
    public String recoverMenus(Integer[] ids){
        return this.menuService.batchRecoverMenus(Arrays.asList(ids))?"success":"fail";
    }

}
